<?php
/*
 * 公司视频
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\Session;
use app\common\model\Video as VideoModel;

class Video extends AdminBase
{
    public function lists()
    {
        $param = $this->request->param();
        if (!empty($param['title'])) {
            $where = function ($query) use ($param){
                $query->where('name','LIKE', '%'.trim($param['title']).'%');
            };
        }else{
            $where = 1;
        }
        $list = VideoModel::where($where)->withAttr('type', function($value) {  // 动态获取器
                    $type = ["全部视频","消费者","创业者","供应商","小伙伴"];
                    return $type[$value];
                })->paginate();
        return $this->fetch('index',['list'=>$list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = VideoModel::create($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('添加了视频');
            $this->success('添加成功');
        }
        return $this->fetch('save');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = VideoModel::update($param);
            !$result && $this->error($this->errorMsg);
            insert_admin_log('修改了视频');
            $this->success('修改成功');
        }
        return $this->fetch('save',['data' => Db::table('video')->find(input('id'))]);
    }

    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            // Db::table('goods')->where('supplier_id',$param['id'])->find() && $this->error('供应商下有商品，无法删除');
            VideoModel::destroy($param['id']);
            insert_admin_log('删除了视频');
            $this->success('删除成功');
        }
    }



}
